package edu.fasta.sisinfo2.gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import edu.fasta.sisinfo2.net.DatosUsuario;
import edu.fasta.sisinfo2.net.InterfazComandoServer;
import edu.fasta.sisinfo2.net.NewUserVerify;
import edu.fasta.sisinfo2.poker.PokerControllerCliente;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.Color;

public class SignUp extends JDialog {

	private final JPanel contentPanel = new JPanelFondo("/Img/Login3.png",0,0);
	private JTextField textField;
	private JPasswordField passwordField;
	private JPasswordField confirmField;
	private JButton okButton = new JButton();
	private JButton cancelButton = new JButton();
	private String userName;
	private String password;
	private PokerControllerCliente PCC;
	private InterfazComandoServer comando;
	private Icon c1 = new ImageIcon(getClass().getResource( "/Img/crear1.png" ));
	private Icon c2 = new ImageIcon(getClass().getResource( "/Img/crear2.png" ));
	private Icon c3 = new ImageIcon(getClass().getResource( "/Img/crear3.png" ));
	private Icon s1 = new ImageIcon(getClass().getResource( "/Img/Salir1.png" ));
	private Icon s2 = new ImageIcon(getClass().getResource( "/Img/Salir2.png" ));
	private Icon s3 = new ImageIcon(getClass().getResource( "/Img/Salir3.png" ));

	/**
	 * Create the dialog.
	 */
	public SignUp(PokerControllerCliente unPCC) {
		this.setPCC(unPCC);
		setTitle("Texas UFasta - Registro");
		setBounds(100, 100, 321, 218);
		this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
		this.setResizable(false);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		{
			JLabel lblNombreDeUsuario = new JLabel("Usuario");
			lblNombreDeUsuario.setBounds(58, 41, 55, 20);
			contentPanel.add(lblNombreDeUsuario);
		}
		{
			textField = new JTextField();
			textField.setBounds(125, 41, 173, 20);
			contentPanel.add(textField);
			textField.setColumns(10);
		}
		{
			JLabel lblPassword = new JLabel("Password");
			lblPassword.setBounds(53, 73, 65, 20);
			contentPanel.add(lblPassword);
		}
		{
			passwordField = new JPasswordField();
			passwordField.setBounds(125, 73, 173, 20);
			contentPanel.add(passwordField);
		}
		{
			JLabel lblIngresarPasswordNuevamente = new JLabel("Confirmar Clave");
			lblIngresarPasswordNuevamente.setBounds(40, 107, 90, 20);
			contentPanel.add(lblIngresarPasswordNuevamente);
		}
		
		{
			JLabel lblCreacionDeNuevo = new JLabel("Formulario de alta de usuario");
			lblCreacionDeNuevo.setForeground(Color.WHITE);
			lblCreacionDeNuevo.setFont(new Font("Tahoma", Font.BOLD, 11));
			lblCreacionDeNuevo.setBounds(69, 11, 186, 22);
			contentPanel.add(lblCreacionDeNuevo);
		}
		{
			okButton = new JButton( "", c1 );
			okButton.setEnabled(false);
			okButton.setRolloverIcon( c2 );
			okButton.setPressedIcon(c3);
			okButton.setBorder(BorderFactory.createEmptyBorder());
			okButton.setContentAreaFilled(false);
			okButton.setBounds(43, 139, 114, 39);
			contentPanel.add(okButton);
			okButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					setUserName(getTextField().getText());
					setPassword(getPasswordField().getText());
					try{
						getPCC().enviarComando(new DatosUsuario(getUserName(), getPassword(),2));
						setComando(getPCC().recibirComando());
						if ((((NewUserVerify)comando).getTipomensaje())==1){
							comando.ejecutarComando(getPCC().getPantalla());
							getPCC().getCliente().cerrarConexion();
							getPCC().getCliente().iniciar();
							dispose();
						}else{
							((NewUserVerify)comando).ejecutarComandoError(getPCC().getPantalla());
							//resetear conexion
							getPCC().getCliente().cerrarConexion();
							getPCC().getCliente().iniciar();
						}
						
						dispose();
					}catch(Exception e){
						System.out.println("\n error en la conexion! ");
					}finally{}
					
				}
			});
			okButton.setActionCommand("OK");
			getRootPane().setDefaultButton(okButton);
		}
		{
			cancelButton = new JButton( "", s1 );
			cancelButton.setRolloverIcon( s2 );
			cancelButton.setPressedIcon(s3);
			cancelButton.setBorder(BorderFactory.createEmptyBorder());
			cancelButton.setContentAreaFilled(false);
			cancelButton.setBounds(173, 139, 114, 39);
			contentPanel.add(cancelButton);
			cancelButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					dispose();
				}
			});
			cancelButton.setActionCommand("Cancel");
		}
		{
			confirmField = new JPasswordField();
			confirmField.addKeyListener(new KeyAdapter() {
				@Override
				public void keyReleased(KeyEvent arg0) {
					if(passwordField.getText().equals(confirmField.getText())){
						okButton.setEnabled(true);						
					}else{
						okButton.setEnabled(false);
					}
				}
			
				
			});
			confirmField.setBounds(125, 107, 173, 20);
			contentPanel.add(confirmField);
		}
	}
	
	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public PokerControllerCliente getPCC() {
		return PCC;
	}

	public void setPCC(PokerControllerCliente pCC) {
		PCC = pCC;
	}

	public JTextField getTextField() {
		return textField;
	}

	public void setTextField(JTextField textField) {
		this.textField = textField;
	}

	public JPasswordField getPasswordField() {
		return passwordField;
	}

	public void setPasswordField(JPasswordField passwordField) {
		this.passwordField = passwordField;
	}

	public InterfazComandoServer getComando() {
		return comando;
	}

	public void setComando(InterfazComandoServer comando) {
		this.comando = comando;
	}
	
}
